<?php

declare(strict_types=1);

namespace App\Model\PssModel;

use App\Model\BaseModel\BasePssModel;
class MerchantGoodsList extends BasePssModel
{
    protected $table = 'goods_merchant_list';
    public $goodsType = [
        0 => '一般贸易',
        1 => '保税品',
        2 => '海外直邮'
    ];
    
    //指定写入字段
    protected $fillable = [
        'id', 
        'goods_id', 
        'skucode', 
        'store_id',
        'company_name',
        'commodity_type',
        'spec_list',
        'price',
        'price_settlement',
        'price_market',
        'price_vip',
    ];
    
    //指定字段展示类型
    protected $casts = [
        'id'            => 'string', 
        'commodify_id'  => 'string',
        'goods_id'      => 'string',
        'store_id'      => 'string',
        'company_id'    => 'string',
    ];
    
    /**
     * 获取授权对应类型分页数据
     * @param array $param
     * @return mixed
     */
    public function getPageGoodsList($page,$limit,$where)
    {
        return self::query()
        ->where($where)
        ->offset($page)
        ->limit($limit)
        ->get()
        ->toArray();
    }
    
    /**
     * 获取重组商品数据
     * @param array $goodsList
     */
    public function getPushGoodsList( $goodsList = [] )
    {
        $temp = [];
        foreach ($goodsList as $item )
        {
            $storeList = @json_decode($item['spec_list'],true);
            $storeList = array_map(function ( & $value){
                $value['goods_batch_list'] = array_map(function ( & $value) {
                    $value['spec_list'] = array_map(function ( & $value) {
                        unset($value['priceCost']);
                        return $value;
                    },$value['spec_list']);
                        return $value;
                },$value['goods_batch_list']);
                    return $value;
            }, $storeList);
    
            $commodity_type = $item['commodity_type'];
            $commodity_type_name = isset($this->goodsType[$commodity_type]) ? $this->goodsType[$commodity_type] : '';
            $temp[] =[
                'goodsId'           => (string)$item['commodify_id'],
                'goodsName'         => $item['commodity_name'],
                'introName'         => $item['intro_name'],
                'images'            => @json_decode($item['image']),
                'description'       => $item['description'],
                'goodsType'         => $commodity_type,
                'goodsTypeName'     => $commodity_type_name,
                'status'            => 1,
                'categoryId'        => (string)$item['fclass_id'],
                'categoryCode'      => $item['fclass_code'],
                'categoryName'      => $item['fclass_name'],
                'categoryPid'       => (string)$item['sclass_id'],
                'categoryPreCode'   => $item['tclass_code'],
                'categoryPreName'   => $item['sclass_name'],
                'categoryFPid'      => (string)$item['tclass_id'],
                'categoryFPreCode'  => $item['sclass_code'],
                'categoryFPreName'  => $item['tclass_name'],
                'categoryPreId'     => (string)$item['tclass_id'],
                'brandID'           => (string)$item['brand_id'],
                'brandCode'         => $item['brand_code'],
                'brandName'         => $item['brand_name'],
                'countryId'         => (string)$item['country_id'],
                'countryCode'       => $item['country_code'],
                'countryName'       => $item['country_name'],
                'skuList'           => [
                    [
                        'skuId'         => (string)$item['goods_id'],
                        'skucode'       => $item['skucode'],
                        'barcode'       => $item['barcode'],
                        'tax_rate'      => $item['tax_rate'],
                        'status'        => 1,
                        'price'         => $item['price'],
                        'price_cost'    => $item['price_settlement'],
                        'price_market'  => $item['price_market'],
                        'specList'      => [],
                        'storeList'     => $storeList
                    ]
                ]
            ];
        }
        return $temp;
    }
    
    /**
     * 获取商品授权清单
     * @param array $param
     */
    public function getUserAuthGoodsList( $param = [] )
    {
        return $this->select([
            'goods_id',
            'skucode',
            'barcode',
            'spec_list'
        ])
        ->where('bid',$param['bid'])
        ->whereIn('skucode',$param['skuCodes'])
        ->get()
        ->toArray();
    }
}
